package com.shanyue.redwine.utils.RecyclerView;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shanyue.redwine.R;
import com.shanyue.redwine.entity.Page;
import com.shanyue.redwine.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ 创建 huangxiaoyu
 * @创建时间 2018/8/29 9:30
 * @描述
 */
public abstract class CommonLoadMoreAdapter<T> extends MultiItemTypeAdapter<T> {
    static final int ITEM_TYPE_NORMAL = 1;
    static final int ITEM_TYPE_FOOTER = 2;
    static final int ITEM_TYPE_HEADER = 0;
    boolean loadEnd = false;//是否加载完全部数据

    protected Context mContext;
    protected int mLayoutId;
    protected List<T> mDatas;
    protected LayoutInflater mInflater;

    public CommonLoadMoreAdapter(final Context context, final int layoutId) {
        this(context, layoutId, new ArrayList<T>());
    }


    public CommonLoadMoreAdapter(final Context context, final int layoutId, List<T> datas) {
        super(context, datas);
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mLayoutId = layoutId;
        mDatas = datas;
        addItemViewDelegate(ITEM_TYPE_NORMAL, new ItemViewDelegate<T>() {
            @Override
            public int getItemViewLayoutId() {
                return layoutId;
            }

            @Override
            public boolean isForViewType(T item, int position) {
                return !isFooterView(position);
            }

            @Override
            public void convert(BaseViewHolder holder, T t, int position) {
                CommonLoadMoreAdapter.this.convert(holder, getDatas().get(position), position);
            }
        });
        addItemViewDelegate(ITEM_TYPE_FOOTER, new ItemViewDelegate<T>() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.view_load_more;
            }

            @Override
            public boolean isForViewType(T item, int position) {
                return isFooterView(position);
            }

            @Override
            public void convert(BaseViewHolder holder, T t, int position) {
                View view = holder.getView(R.id.pbBar);
                if (!loadEnd) {
                    view.setVisibility(View.VISIBLE);
                    holder.setText(R.id.tvHint, "数据加载中...");
                } else {
                    view.setVisibility(View.GONE);
                    holder.setText(R.id.tvHint, "已加载全部");
                }
            }
        });
    }


    protected abstract void convert(BaseViewHolder holder, T t, int position);

    @Override
    public int getItemCount() {
        return super.getItemCount() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (mDatas.size() == 0 && isFooterView(position)) {
            return ITEM_TYPE_FOOTER;
        }
        return ITEM_TYPE_NORMAL;
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) layoutManager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if (isFooterView(position)) {
                        return gridManager.getSpanCount();
                    }
                    return 1;
                }
            });
        }

    }

    @Override
    public void onViewAttachedToWindow(@NonNull BaseViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        if (isFooterView(holder.getLayoutPosition())) {
            ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();

            if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) {
                StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
                p.setFullSpan(true);
            }
        }
    }


    private boolean isFooterView(int position) {
        LogUtils.d("getItemCount:" + getItemCount());
        return position >= getItemCount() - 1;
    }

    /**
     * 数据是否全部加载完成
     *
     * @param loadEnd
     */
    public void setLoadEnable(boolean loadEnd) {
        this.loadEnd = loadEnd;
        notifyItemChanged(getItemCount() - 1);
    }

    @Override
    public void refresh(List data) {
        this.loadEnd = data.size() < Page.DEFAULT_PAGE_SIZE ? true : false;//数据是否全部加载完成
        super.refresh(data);
    }

    @Override
    public void append(List data) {
        this.loadEnd = data.size() < Page.DEFAULT_PAGE_SIZE ? true : false;//数据是否全部加载完成
        super.append(data);
    }

}
